﻿using System;
using System.Text;

namespace PasteLoggerBoard
{
    /// <summary>
    /// redis的链接信息
    /// </summary>
    public class RedisConfig
    {

        /// <summary>
        /// 主链接
        /// </summary>
        public string MainConnection { get; set; }

        /// <summary>
        /// 前缀
        /// </summary>
        public string Prefix { get; set; }
    }


    /// <summary>
    /// 
    /// </summary>
    public class PublicString
    {
        /// <summary>
        /// 图形二维码的前缀 cache:image:
        /// </summary>
        public const string CacheImageHead = "cache:image:";

        /// <summary>
        /// user.token密钥
        /// </summary>
        public const string TokenKey = "1111-2222-333333-444444-5555-6666";

        /// <summary>
        /// 密钥名称
        /// </summary>
        public const string TokenHeadName = "token";

        /// <summary>
        /// 角色缓存的权限列表 cache:item:grade:{gradecode}
        /// </summary>
        public const string CacheGradeInfoHead = "cache:item:grade:{0}";

        /// <summary>
        /// 密钥过期时间
        /// </summary>
        public const int TokenTimeOut = 3600 * 24 * 7;

        /// <summary>
        /// report:hash:user yyyyMMdd_userid_type value
        /// </summary>
        public const string CacheReportUserKey = "report:hash:user";

        /// <summary>
        /// report:hash:user yyyyMMdd_projectid_type value
        /// </summary>
        public const string CacheReportProjectKey = "report:hash:project";

        /// <summary>
        /// 我的下级账号列表 cache:next:user:{0}
        /// </summary>
        public const string CacheMyNextUserList = "cache:next:user:{0}";

        /// <summary>
        /// 
        /// </summary>
        public const string _cacheheadproject = "cache:item:project:";

        /// <summary>
        /// 
        /// </summary>
        public const string _cacheheadorder = "cache:item:order:";

        /// <summary>
        /// 
        /// </summary>
        public const string _cacheheadpart = "cache:item:part:";

        /// <summary>
        /// 
        /// </summary>
        public const string _cacheheaduser = "cache:item:user:";

        /// <summary>
        /// hashset 统计ip访问次数
        /// yyyyMMdd
        /// field:ip
        /// value:time
        /// </summary>
        public const string CollectClientIp = "collect:hash:clientip:";

        /// <summary>
        /// hashset 统计访问信息
        /// yyyyMMdd
        /// field:AppName_HH_Level
        /// value:time
        /// </summary>
        public const string CollectLevel = "collect:hash:visit:";

        /// <summary>
        /// hashset 统计状态码
        /// yyyyMMdd
        /// field:AppName_HH_StatusCode
        /// value:time
        /// </summary>
        public const string CollectStatus = "collect:hash:status:";

        /// <summary>
        /// hashset 统计状态码
        /// yyyyMMdd
        /// field:AppName_HH_Path
        /// value:time
        /// </summary>
        public const string CollectPath = "collect:hash:path:";

        /// <summary>
        /// clientip的统计 小时的数据 双统计 满100回收一次 满小时回收一次?
        /// field:yyyyMMddHH
        /// value:112233
        /// </summary>
        public const string HashIncrement = "data:increment:clientip";
    }

    /// <summary>
    /// 
    /// </summary>
    public static class ObjectExtend
    {
        /// <summary>
        /// 转化成小写的md5
        /// </summary>
        /// <param name="inputValue"></param>
        /// <returns></returns>
        public static string ToMd5Lower(this string inputValue)
        {

            using (var md5 = System.Security.Cryptography.MD5.Create())
            {
                var result = md5.ComputeHash(Encoding.UTF8.GetBytes(inputValue));
                var strResult = BitConverter.ToString(result);
                return strResult.Replace("-", "").ToLower();
            }
        }

        /// <summary>
        /// 自动切割
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string AutoSubString(this string str, int len)
        {
            if (!String.IsNullOrEmpty(str))
            {
                if (str.Length > len)
                {
                    return str[..len];
                }
            }
            return str;
        }

        /// <summary>
        /// 时间转时间戳s ok
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ToUnixTimeSeconds(this DateTime dt)
        {
            var dto = new DateTimeOffset(dt);
            return new DateTimeOffset(dt).ToUnixTimeSeconds();
        }
    }
}
